package vip.idiy.anwei_android_iot.utils;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Build.VERSION_CODES.M;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Process;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * ClassName PermissionUtil
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 15:09 2023/4/22
 */
public class PermissionUtil {
    /**
     * 请求单个权限
     *
     * @param activity   当前activity
     * @param permission 权限名
     * @param callback   回调函数，返回值为PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED
     */
    public static void request(Activity activity, String permission, Consumer<Integer> callback) {
        request2(activity, permission, callback);
    }

    /**
     * 请求多个权限
     *
     * @param activity    当前activity
     * @param permissions 权限名数组
     * @param callback    回调函数，返回值为PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED
     */
    public static void request(Activity activity, String[] permissions, Consumer<Integer> callback) {
        request2(activity, permissions, callback);

    }

    /**
     * 请求单个权限
     *
     * @param activity   当前activity
     * @param permission 权限名
     * @param callback   回调函数，返回值为PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED
     */
    public static void request2(Activity activity, String permission, Consumer<Integer> callback) {
        if (!has(activity, permission)) {
            ActivityCompat.requestPermissions(activity, new String[]{permission}, 1);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                callback.accept(PERMISSION_GRANTED);
            }
        }
    }

    /**
     * 请求多个权限
     *
     * @param activity    当前activity
     * @param permissions 权限名数组
     * @param callback    回调函数，返回值为PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED
     */
    public static void request2(Activity activity, String[] permissions, Consumer<Integer> callback) {
        List<String> mPermissionListDenied = new ArrayList<>();
        for (String permission : permissions) {
            int result = checkPermission(activity, permission);
            if (result != PERMISSION_GRANTED) {
                mPermissionListDenied.add(permission);
            }
        }
        if (mPermissionListDenied.size() > 0) {
            ActivityCompat.requestPermissions(activity, mPermissionListDenied.toArray(new String[mPermissionListDenied.size()]), 1);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                callback.accept(PERMISSION_GRANTED);
            }
        }
    }

    /**
     * 判断是否拥有权限
     *
     * @param activity    当前activity
     * @param permissions 权限名数组
     * @return 是否拥有权限
     */
    private static boolean has(Context activity, String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否拥有权限
     *
     * @param context    当前context
     * @param permission 权限名
     * @return 是否拥有权限
     */
    private static boolean has(Context context, String permission) {
        return context.checkPermission(permission, Process.myPid(), Process.myUid()) == PERMISSION_GRANTED;
    }

    /**
     * 检查权限
     *
     * @param activity   当前activity
     * @param permission 权限名
     */
    @RequiresApi(api = M)
    private static int checkPermission(Activity activity, String permission) {
        if (ContextCompat.checkSelfPermission(activity, permission) == PERMISSION_GRANTED) {
            return PERMISSION_GRANTED;
        } else {
            activity.requestPermissions(new String[]{permission}, 1);
            return ContextCompat.checkSelfPermission(activity, permission);
        }
    }

    /**
     * 检查权限
     *
     * @param activity    当前activity
     * @param permissions 权限名数组
     */
    @RequiresApi(api = M)
    private static void checkPermissions(Activity activity, String[] permissions) {
        List<String> mPermissionListDenied = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(activity, permission) != PERMISSION_GRANTED) {
                mPermissionListDenied.add(permission);
            }
        }
        if (mPermissionListDenied.size() > 0) {
            activity.requestPermissions(mPermissionListDenied.toArray(new String[mPermissionListDenied.size()]), 1);
        }
    }
}